package HTTPServer

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
)

/*
HttpQinYun

	返回一个map：{"result":0,"content":"恩！"}恩！

body, _ := ioutil.ReadAll(res.Body)

	bodystr := string(body)
	//对返回的json数据做解析
	var dataAttr map[string]interface{}
	var code string
	var username string
	if err := json.Unmarshal([]byte(bodystr), &dataAttr); err == nil {
	    for idx, value := range dataAttr {
	        fmt.Println(idx)
	        fmt.Println(value)
	        if (idx == "code") {
	            code = fmt.Sprintf("%v", value)
	        }
	        if (idx == "data") {
	            mapTmp := value.(map[string]interface{})
	            username = mapTmp["username"].(string)
	        }
	    }
	} else {
	    fmt.Println("json解析出错:");
	    fmt.Println(err.Error());
	}
	//返回
	m := map[string]interface{} {
	    "resCode":code,
	    "username":username,
	}
	resp := result.NewResult(c)
	resp.Success(m)
*/
func HttpQinYun(S string) (map[string]string, error) {
	var result map[string]string
	//http://api.qingyunke.com/api.php?key=free&appid=0&msg=你好
	s := string(url.QueryEscape(S))
	response, err := http.Get("http://api.qingyunke.com/api.php?key=free&appid=0&msg=" + s)
	if err != nil {
		log.Fatal(err)
		return result, err
	}
	ios, err := ioutil.ReadAll(response.Body)
	if err != nil {
		log.Fatal(err)
		return result, err
	}
	//将字符串转成map

	json.Unmarshal([]byte(string(ios)), &result)
	//输出map中的result
	return result, err
}
